<template>
  <div class="page-content">
    <div class="header">
      <div class="header-title">个人中心</div>
      <div class="header-right">
        <span class="settings-icon" @click="goToSettings">⚙️</span>
      </div>
    </div>
    
    <div class="container">
      <!-- 用户信息卡片 -->
      <div class="user-card">
        <img :src="currentUser.avatar" alt="用户头像" class="user-avatar" />
        <div class="user-info">
          <div class="username">{{ currentUser.username }}</div>
          <div class="user-bio">{{ currentUser.bio }}</div>
        </div>
      </div>
      
      <!-- 统计信息 -->
      <div class="stats-section">
        <div class="stat-item">
          <div class="stat-number">{{ userStats.checkInCount }}</div>
          <div class="stat-label">打卡</div>
        </div>
        <div class="stat-divider"></div>
        <div class="stat-item">
          <div class="stat-number">{{ userStats.likeCount }}</div>
          <div class="stat-label">获赞</div>
        </div>
        <div class="stat-divider"></div>
        <div class="stat-item">
          <div class="stat-number">{{ userStats.followingCount }}</div>
          <div class="stat-label">关注</div>
        </div>
        <div class="stat-divider"></div>
        <div class="stat-item">
          <div class="stat-number">{{ userStats.followerCount }}</div>
          <div class="stat-label">粉丝</div>
        </div>
      </div>
      
      <!-- 我的打卡 -->
      <div class="my-checkins-section">
        <div class="section-header">
          <div class="section-title">我的打卡</div>
        </div>
        
        <div v-if="loading" class="loading">加载中...</div>
        
        <div v-else-if="userCheckIns.length === 0" class="empty">
          暂无打卡记录，快去发布第一条吧！
        </div>
        
        <div v-else class="my-checkins-list">
          <div v-for="checkIn in userCheckIns" :key="checkIn.id" class="my-checkin-item" @click="goToDetail(checkIn.id)">
            <!-- 图片预览 -->
            <div v-if="checkIn.imageUrls.length > 0" class="checkin-preview">
              <img :src="checkIn.imageUrls[0]" alt="打卡预览" />
              <div v-if="checkIn.imageUrls.length > 1" class="image-count">
                {{ checkIn.imageUrls.length }}
              </div>
            </div>
            
            <!-- 内容预览 -->
            <div class="checkin-preview-content">
              <div class="content-preview">{{ truncateText(checkIn.content, 50) }}</div>
              <div class="checkin-stats">
                <span class="stat">❤️ {{ checkIn.likeCount || 0 }}</span>
                <span class="stat">💬 {{ checkIn.commentCount || 0 }}</span>
                <span class="time">{{ checkIn.formattedTime }}</span>
              </div>
            </div>
          </div>
        </div>
        
        <!-- 加载更多 -->
        <div v-if="hasMore" class="load-more" @click="loadMore">
          加载更多
        </div>
        
        <!-- 发布按钮 -->
        <div class="publish-btn-container">
          <router-link to="/upload" class="publish-btn">
            <span class="btn-icon">+</span>
            <span class="btn-text">发布打卡</span>
          </router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { mock } from '../utils/mockData.js'

export default {
  name: 'Profile',
  setup() {
    const router = useRouter()
    const currentUser = ref(mock.currentUser)
    const userStats = ref({
      checkInCount: 0,
      likeCount: 0,
      followingCount: 28,
      followerCount: 56
    })
    const userCheckIns = ref([])
    const loading = ref(false)
    const page = ref(1)
    const pageSize = 10
    const hasMore = ref(true)
    
    // 加载用户打卡记录
    const loadUserCheckIns = async (isLoadMore = false) => {
      try {
        if (!isLoadMore) {
          loading.value = true
        }
        
        // 使用mock数据
        const result = mock.getUserCheckIns(currentUser.value.id, isLoadMore ? page.value + 1 : 1, pageSize)
        
        if (isLoadMore) {
          userCheckIns.value = [...userCheckIns.value, ...result.list]
          page.value += 1
        } else {
          userCheckIns.value = result.list
          // 更新打卡数统计
          userStats.value.checkInCount = result.total
        }
        
        hasMore.value = userCheckIns.value.length < result.total
        
        // 计算获赞总数
        calculateTotalLikes()
      } catch (error) {
        console.error('加载用户打卡记录失败:', error)
      } finally {
        loading.value = false
      }
    }
    
    // 计算获赞总数
    const calculateTotalLikes = () => {
      let totalLikes = 0
      userCheckIns.value.forEach(checkIn => {
        totalLikes += checkIn.likeCount || 0
      })
      userStats.value.likeCount = totalLikes
    }
    
    // 加载更多
    const loadMore = () => {
      if (!loading.value && hasMore.value) {
        loadUserCheckIns(true)
      }
    }
    
    // 跳转到详情页
    const goToDetail = (id) => {
      router.push(`/detail/${id}`)
    }
    
    // 跳转到设置页
    const goToSettings = () => {
      alert('设置页面暂未实现')
    }
    
    // 截断文本
    const truncateText = (text, maxLength) => {
      if (!text || text.length <= maxLength) {
        return text
      }
      return text.substring(0, maxLength) + '...'
    }
    
    // 页面加载时获取数据
    onMounted(() => {
      loadUserCheckIns()
    })
    
    return {
      currentUser,
      userStats,
      userCheckIns,
      loading,
      hasMore,
      loadMore,
      goToDetail,
      goToSettings,
      truncateText
    }
  }
}
</script>

<style scoped>
.user-card {
  display: flex;
  align-items: center;
  background-color: white;
  padding: 20px;
  border-radius: 12px;
  margin-bottom: 16px;
}

.user-avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  object-fit: cover;
}

.user-info {
  margin-left: 20px;
  flex: 1;
}

.username {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 8px;
}

.user-bio {
  font-size: 14px;
  color: var(--text-secondary);
}

.stats-section {
  display: flex;
  background-color: white;
  border-radius: 12px;
  margin-bottom: 16px;
  padding: 16px 0;
}

.stat-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-number {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 12px;
  color: var(--text-secondary);
}

.stat-divider {
  width: 1px;
  background-color: #f0f0f0;
}

.my-checkins-section {
  background-color: white;
  border-radius: 12px;
  padding: 16px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
}

.my-checkins-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.my-checkin-item {
  display: flex;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid #f5f5f5;
}

.my-checkin-item:last-child {
  border-bottom: none;
}

.checkin-preview {
  width: 100px;
  height: 100px;
  border-radius: 8px;
  overflow: hidden;
  position: relative;
}

.checkin-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.image-count {
  position: absolute;
  bottom: 4px;
  right: 4px;
  background-color: rgba(0, 0, 0, 0.6);
  color: white;
  padding: 2px 6px;
  border-radius: 10px;
  font-size: 12px;
}

.checkin-preview-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.content-preview {
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-color);
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

.checkin-stats {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: var(--text-light);
  gap: 12px;
}

.time {
  margin-left: auto;
}

.load-more {
  text-align: center;
  padding: 16px;
  color: var(--text-light);
  font-size: 14px;
  cursor: pointer;
}

.load-more:active {
  background-color: #f5f5f5;
}

.publish-btn-container {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #f5f5f5;
}

.publish-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: var(--primary-color);
  color: white;
  padding: 12px;
  border-radius: 25px;
  text-decoration: none;
  font-size: 16px;
}

.btn-icon {
  font-size: 20px;
  margin-right: 8px;
}
</style>